package com.lingxu.base.system.manage.controller;

import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.common.api.vo.ResultTwo;
import com.lingxu.base.system.manage.entity.SysMenu;
import com.lingxu.base.system.manage.entity.SysRoleMenuUserVo;
import com.lingxu.base.system.manage.model.SysMenuTree;
import com.lingxu.base.system.manage.model.TreeNode;
import com.lingxu.base.system.manage.service.ISysMenuService;
import com.lingxu.base.system.manage.service.ISysUserRoleService;
import com.lingxu.base.system.manage.util.TreeNodeUtil;
import com.lingxu.base.system.manage.vo.SysOrgMenuOption;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 菜单信息
 * @Author: LingXu
 * @Date:   2021-01-04
 * @Version: V1.0.1
 */
@Slf4j
@Api(tags="菜单信息")
@RestController
@RequestMapping("/system/manage/sysMenu")
public class SysMenuController{
	@Resource
	private ISysMenuService sysMenuService;
	@Resource
	private ISysUserRoleService sysUserRoleService;



	/**
	 * 菜单信息-系统管理-单位组织菜单树查询
	 *
	 * @return
	 */
	@ApiOperation(value="菜单信息-系统管理-菜单树查询", notes="菜单信息-系统管理-菜单树查询")
	@PostMapping(value = "/treeListInManage")
	public ResultTwo<SysMenuTree> getTreeListInManage(@RequestBody SysOrgMenuOption sysOrgMenuOption) {
		List<TreeNode> treeNodeList=new ArrayList<>();
		List<TreeNode> newTreeNode=new ArrayList<>();
		TreeNodeUtil treeUtil=new TreeNodeUtil<>();

		sysOrgMenuOption.setIsFlag(true);
		List<SysMenuTree> sysMenuTreeList=sysMenuService.getTreeListInManage(sysOrgMenuOption);
		treeNodeList.addAll(sysMenuTreeList);

		sysOrgMenuOption.setIsFlag(false);
		List<SysMenuTree> newMenuTreeList=sysMenuService.getTreeListInManage(sysOrgMenuOption);
		newTreeNode.addAll(newMenuTreeList);

		List<TreeNode> result=treeUtil.list2TreeNode(treeNodeList);
		List<TreeNode> result2=treeUtil.list2TreeNode(newTreeNode);
		return ResultTwo.ok(result,result2);
	}


	/**
	 * 用户信息-系统管理-为用户分配全部角色
	 *
	 * @return 角色信息-系统管理-为用户分配全部角色
	 */
	@ApiOperation(value="角色信息-系统管理-为用户分配全部角色", notes="角色信息-系统管理-为用户分配全部角色")
	@PostMapping("/distributeUserRolesAll")
	public Result<?> distributeUserRolesAll(@RequestParam(name="userId")@ApiParam(value = "用户id",required = true) String userId){
		sysUserRoleService.distributeUserRolesAll(userId);
		return Result.ok("分配成功");
	}

	/**
	 * 用户信息-系统管理-为用户分配角色
	 *
	 * @return 角色信息-系统管理-为用户分配角色
	 */
	@ApiOperation(value="角色信息-系统管理-为用户分配角色", notes="角色信息-系统管理-为用户分配角色")
	@PostMapping("/distributeUserRoles")
	public Result<?> distributeUserRoles(@RequestParam(name="userId")@ApiParam(value = "用户id",required = true) String userId, @RequestParam(name="roleIds")@ApiParam(value = "角色ids(多个用逗号','隔开)",required = true) String roleIds){
		String[] roleIdStr = roleIds.split(",");
		List<String> roleIdList = Arrays.asList(roleIdStr);
		sysUserRoleService.distributeUserRoles(userId,roleIdList);
		return Result.ok("分配成功");
	}

	/**
	 * 用户信息-系统管理-为用户取消角色
	 *
	 * @return
	 */
	@ApiOperation(value="角色信息-系统管理-为用户取消角色", notes="角色信息-系统管理-为用户取消角色")
	@PostMapping("/cancelUserRoles")
	public Result<?> cancelUserRoles(@RequestParam(name="userId")@ApiParam(value = "用户id",required = true) String userId,@RequestParam(name="roleIds")@ApiParam(value = "角色ids(多个用逗号','隔开)",required = true) String roleIds){
		sysUserRoleService.cancelUserRoles(userId,roleIds);
		return Result.ok("取消成功");
	}



	/**
	 * 用户信息-系统管理-为用户取消角色
	 *
	 * @return
	 */
	@ApiOperation(value="角色信息-系统管理-为用户取消全部角色", notes="角色信息-系统管理-为用户取消全部角色")
	@PostMapping("/cancelUserRolesAll")
	public Result<?> cancelUserRolesAll(@RequestParam(name="userId")@ApiParam(value = "用户id",required = true) String userId){
		sysUserRoleService.cancelUserRolesAll(userId);
		return Result.ok("取消成功");
	}

	@ApiOperation(value="菜单管理-新增菜单", notes="菜单管理-新增菜单")
	@PostMapping("/addNewSysMenu")
	public Result addNewSysMenu(@RequestBody SysMenu sysMenu){
		if(StringUtils.isEmpty(sysMenu.getPid())){
			sysMenu.setTypes("0");
		}else {
			sysMenu.setTypes("1");
		}
		sysMenu.setIsdel("0");
		sysMenuService.save(sysMenu);
		return Result.ok("操作成功");
	}
	@ApiOperation(value="菜单管理-修改菜单", notes="菜单管理-修改菜单")
	@PostMapping("/updateSysMenu")
	public Result updateSysMenu(@RequestBody SysMenu sysMenu) throws Exception{
		return sysMenuService.updateSysMenu(sysMenu);
	}
	@ApiOperation(value="菜单管理-删除菜单", notes="菜单管理-删除菜单")
	@PostMapping("/deleteSysMenu")
	public Result deleteSysMenu(String ids){
		String[] split = ids.split(",");
		List<String> list = Arrays.asList(split);
		sysMenuService.deleteSysMenu(list);
		return Result.ok("操作成功");
	}

	@ApiOperation(value="菜单管理-查询菜单树", notes="菜单管理-查询菜单树")
	@PostMapping("/findSysMenuList")
	public Result findSysMenuList(){
		return sysMenuService.findSysMenuList();
	}

	@ApiOperation(value="菜单管理-查询菜单详情", notes="菜单管理-查询菜单详情")
	@PostMapping("/findSysMenuById")
	public Result findSysMenuById(@RequestParam(name="id")@ApiParam(value = "id",required = true) String id){
		try {
			return Result.ok(sysMenuService.getById(id),"查询成功");
		}catch (Exception e){
			log.error("数据查询异常",e);
			return Result.error("查询异常");
		}
	}

	@ApiOperation(value="菜单管理-新增菜单对应按钮", notes="菜单管理-新增菜单对应按钮")
	@PostMapping("/addMenuButton")
	public Result addMenuButton(@RequestBody SysMenu menuButton){
		return sysMenuService.addMenuButton(menuButton);
	}

	@ApiOperation(value="菜单管理-修改菜单对应按钮", notes="菜单管理-修改菜单对应按钮")
	@PostMapping(value = "/updateMenuButton")
	public Result updateMenuButton(@RequestBody SysMenu menuButton){
		return sysMenuService.updateMenuButton(menuButton);
	}

	@ApiOperation(value="菜单管理-删除菜单对应按钮", notes="菜单管理-删除菜单对应按钮")
	@PostMapping(value = "/deleteMenuButton")
	public Result deleteMenuButton(@RequestParam(name="id")@ApiParam(value = "id",required = true) String id){
		return sysMenuService.deleteMenuButton(id);
	}

	@ApiOperation(value="菜单管理-查询菜单下按钮", notes="菜单管理-查询菜单下按钮")
	@PostMapping("/findMenuButtonList")
	public Result findMenuButtonList(@RequestParam(name="menuId")@ApiParam(value = "menuId",required = true) String menuId){
		return sysMenuService.findMenuButtonList(menuId);
	}

	@ApiOperation(value="菜单管理-根据类型查询菜单", notes="菜单管理-根据类型查询菜单")
	@PostMapping("/findMenuList")
	public Result findMenuList(@RequestBody SysMenu sysMenu){
		return sysMenuService.findMenuList(sysMenu);
	}

	@ApiOperation(value="菜单管理-根据用户ID和菜单pid获取子菜单", notes="菜单管理-根据用户ID和菜单pid获取子菜单")
	@PostMapping("/findMenuListByUseridAndMenupid")
	public Result findMenuListByUseridAmdMenupid(@RequestBody SysRoleMenuUserVo sysRoleMenuUserVo){
		return sysMenuService.findMenuListByUseridAmdMenupid(sysRoleMenuUserVo);
	}



	/*===============================================================================================*/
	/**
	 * @Author ysj闫世杰
	 * @Description 查询未分配菜单
	 * @Date 2022/3/4 9:32
	 * @Param: roleId
	 * @return com.lingxu.base.common.api.vo.Result
	 **/
	@ApiOperation("查询未分配菜单")
	@RequestMapping(value = "/findRoleNotExistMenu",method = RequestMethod.GET)
	public Result findRoleNotExistMenu(String roleId){
		return sysMenuService.findRoleNotExistMenu(roleId);
	}

	/**
	 * @Author ysj闫世杰
	 * @Description 查询已分配菜单
	 * @Date 2022/3/4 9:32
	 * @Param: roleId
	 * @return com.lingxu.base.common.api.vo.Result
	 **/
	@ApiOperation("查询已分配菜单")
	@RequestMapping(value = "/findRoleExistMenu",method = RequestMethod.GET)
	public Result findRoleExistMenu(String roleId){
		return sysMenuService.findRoleExistMenu(roleId);
	}

	/*===============================================================================================*/
}
